<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/13
 * Time: 9:36
 */

namespace api\storage\model;


use think\Model;

class StaffStorageModel extends Model
{
    protected $table = 'iwh_staff_storage';
    protected $pk = 'id';

    /**
     * 获取用户的当前仓库信息
     *
     * @param int $userId
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findCurrentStorage($userId = 0){
        $where['user_id'] = $userId;
        $where['is_current'] = 1;
        return $this->alias('ss')
            ->join(['iwh_storage'=>'s'],'ss.storage_uuid = s.uuid','left')
            ->where($where)
            ->find();
    }

    /**
     * 获取用户的全部仓库
     *
     * @param int $userId
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryStorage($userId = 0){
        return $this->alias('ss')
            ->field('ss.storage_uuid as storageUuid,s.name')
            ->join(['iwh_storage'=>'s'],'ss.storage_uuid = s.uuid','left')
            ->where('user_id',$userId)
            ->select();
    }

    /**
     * 更新当前仓库信息
     *
     * @param $userId
     * @param $storageUuid
     * @return false|int
     */
    public function updateCurrentStorage($userId, $storageUuid){
        $where['user_id'] = $userId;
        $where['storage_uuid'] = $storageUuid;
        return $this->isUpdate(true)->save(['is_current'=>1],$where);
    }

    /**
     * 更新该仓库不是当前仓库
     *
     * @param $userId
     * @param $storageUuid
     * @return false|int
     */
    public function updateNotCurrentStorage($userId, $storageUuid){
        $where['user_id'] = $userId;
        $where['storage_uuid'] = $storageUuid;
        return $this->isUpdate(true)->save(['is_current'=>0],$where);
    }
}